package com.itheima;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class SearchIndex {
    /**
     * 需求:查询索引库===简单查询
     * @throws Exception
     */
    @Test
    public void test() {
        System.out.println("A程序员写的代码");
        System.out.println("B程序员也开始写代码了");
        System.out.println("B....");
        System.out.println("A.....");
    }
    @Test
    public void simpleQuery() throws Exception {
        //指定solr远程服务连接地址
        String url = "http://localhost:8082/solr/products";
        //创建连接solr服务核心对象
        SolrServer solrServer = new HttpSolrServer(url);
        //创建solrQuery对象,封装所有查询参数
        SolrQuery solrQuery = new SolrQuery();
        //设置简单条件
        //solrQuery.set("q","*:*");
        solrQuery.setQuery("*:*");
        //直接使用solr服务对象,查询索引库
        QueryResponse response = solrServer.query(solrQuery);
        //从响应对象中获取文档集合
        SolrDocumentList results = response.getResults();
        //从文档集合中获取命中总记录数
        long numFound = results.getNumFound();
        System.out.println("命中总记录数:"+numFound);
        //循环文档集合,获取每一个文档对象
        for (SolrDocument doc : results) {
            //根据域字段名称,获取域字段的值
            String id = (String) doc.get("id");
            System.out.println("商品id:"+id);
            String product_name = (String) doc.get("product_name");
            System.out.println("商品名称:"+product_name);
            Float product_price = (Float) doc.get("product_price");
            System.out.println("商品价格:"+product_price);
            String product_description = (String) doc.get("product_description");
            System.out.println("商品描述:"+product_description);
            String product_picture = (String) doc.get("product_picture");
            System.out.println("商品图片:"+product_picture);
            String product_catalog_name = (String) doc.get("product_catalog_name");
            System.out.println("商品分类:"+product_catalog_name);

        }
    }

    /**
     * 需求:查询索引库===复杂查询
     * @throws Exception
     */
    @Test
    public void FuZQuery() throws Exception {
        //指定solr远程服务连接地址
        String url = "http://localhost:8082/solr/products";
        //创建连接solr服务核心对象
        SolrServer solrServer = new HttpSolrServer(url);
        //创建solrQuery对象,封装所有查询参数
        SolrQuery solrQuery = new SolrQuery();
        //设置简单条件
        //solrQuery.set("q","*:*");
        //主查询条件
        solrQuery.setQuery("家天下");
        //fq 过滤查询
        solrQuery.addFilterQuery("product_catalog_name:时尚卫浴");
        solrQuery.addFilterQuery("product_price:[0 TO 20]");
        //排序查询
        solrQuery.setSort("product_price", SolrQuery.ORDER.desc);
        //start rows 分页查询
        solrQuery.setStart(0);
        solrQuery.setRows(10);
        //fl字段过滤映射查询
        //solrQuery.setFields("product_name,product_price");
        //设置默认查询域字段
        solrQuery.set("df","product_keywords");
        //高亮设置
         solrQuery.setHighlight(true);
         solrQuery.addHighlightField("product_name");
         solrQuery.setHighlightSimplePre("<font color='red'>");
         solrQuery.setHighlightSimplePost("</font>");
        //直接使用solr服务对象,查询索引库
        QueryResponse response = solrServer.query(solrQuery);

        //从响应对象中获取文档集合
        SolrDocumentList results = response.getResults();
        //从文档集合中获取命中总记录数
        long numFound = results.getNumFound();
        System.out.println("命中总记录数:"+numFound);
        //得到高亮
        //循环文档集合,获取每一个文档对象
        for (SolrDocument doc : results) {
            //根据域字段名称,获取域字段的值
            String id = (String) doc.get("id");
            System.out.println("商品id:"+id);
            String product_name = (String) doc.get("product_name");
            //获取高亮显示
            Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
            //第一个map的key时id
            Map<String, List<String>> stringListMap = highlighting.get(id);
            //第二个map的key时高亮字段名称
            List<String> list = stringListMap.get("product_name");
            //判断高亮是否存在
            if (list!=null&&list.size()>0) {
                product_name = list.get(0);
            }
            System.out.println("商品名称:"+product_name);
            Float product_price = (Float) doc.get("product_price");
            System.out.println("商品价格:"+product_price);
            String product_description = (String) doc.get("product_description");
            System.out.println("商品描述:"+product_description);
            String product_picture = (String) doc.get("product_picture");
            System.out.println("商品图片:"+product_picture);
            String product_catalog_name = (String) doc.get("product_catalog_name");
            System.out.println("商品分类:"+product_catalog_name);

        }
    }
}
